﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Collections.Specialized;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.Xml.Linq;

namespace MyWindowsMediaPlayer_V2
{
    public class Playlist : CollectionManager
    {
        private string                     playlistDirectory;

        public Playlist(System.Windows.Controls.MediaElement mediaElement)
        {
            mediaInstance = mediaElement;
            playlistDirectory = Directory.GetCurrentDirectory() + "\\Playlists\\";
            if (!Directory.Exists(playlistDirectory))
                Directory.CreateDirectory(playlistDirectory);
        }
        public void incCurrent_Media()
        {
            if (currentMedia == mediaList.Count() - 1)
                currentMedia = 0;
            else
                currentMedia++;
        }
        private void decCurrent_Media()
        {
            if (currentMedia == 0)
                currentMedia = mediaList.Count() - 1;
            else
                currentMedia--;
        }
        public void saveActual_Playlist()
        {
           
            string path = playlistDirectory;
            string filename = "playlist";
            int     i = 1;

            if (mediaList.Count == 0)
                return;
            while (File.Exists(path + filename + ".xml"))
                filename = "playlist" + i++;  
            path += filename + ".xml";
            using (var fs = new FileStream(path, FileMode.Create))
            {
                XmlSerializer save = new XmlSerializer(typeof(ObservableCollection<Media>), new Type[] {typeof(Video), typeof(Music), typeof(Pics)});

                save.Serialize(fs, mediaList);
            }
        }
        public void load_Playlist()
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Selectionnez une playlist";
            ofd.InitialDirectory = playlistDirectory;
            ofd.Filter = "Fichier playlist *.xml|*.xml";

            if (ofd.ShowDialog().ToString().Equals("OK"))
            {
                XDocument xml = XDocument.Load(ofd.FileName);
                List<string> pathList = new List<string>();

                if (mediaList.Count > 0)
                    removeAll_Items();
                foreach (XElement item in xml.Descendants("Media"))
                    pathList.Add(item.Element("path").Value);
                addMediaTo_List(pathList.ToArray());
            }
        }
/*
**      GETTERS
*/
        public void getNext_Media()
        {
            incCurrent_Media();
            playActual_Media();
        }
        public void getPrevious_Media()
        {
            decCurrent_Media();
            playActual_Media();
        }
        public void playActual_Media()
        {
            if (currentMedia >= 0 && currentMedia < mediaList.Count)
                mediaList[currentMedia].play_Media();
        }
        public void stopActual_Media()
        {
            mediaList[currentMedia].stop_Media();
        }
    }
}
